<?php
namespace app\admin\validate;

use think\Validate;
use app\Exception\ParamException;

class BaseValidate extends validate
{
    public function checkParams(){
        $param = request()->param();
        if($this->batch()->check($param))
            return true;
        else
            throw new ParamException(['errorMsg' => $this->getError()]); 
    }

    /**
     *  整数验证规则
    */
    protected function isPositiveInteger($value, $rule = '', $data = '', $field = '')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return $field . '必须是正整数';
    }

    public function getDataByRule($arrays)
    {
        if (array_key_exists('user_id', $arrays) | array_key_exists('uid', $arrays)) {
            // 不允许包含user_id或者uid，防止恶意覆盖user_id外键
            throw new ParamException([ 'msg' => '参数中包含有非法的参数名user_id或者uid' ]);
        }
        $newArray = [];

        foreach ($this->rule as $key => $value) {
            if(is_array($arrays[$key])){
                foreach ($arrays[$key] as $item){
                    // return $item;
                    $newArray[$key][] = htmlspecialchars(stripslashes(trim($item)));
                }
                
            }else{
                $newArray[$key] = htmlspecialchars(stripslashes(trim($arrays[$key])));
            }
        }

        return $newArray;
    }

}